﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using GomMinhLongBL;
using GomMinhLongDO;

namespace GomMinhLong.AdminCP.PageUC
{
    public partial class ucListAlbum : System.Web.UI.UserControl
    {
        private static int type = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["SanPham"] == null)
                {
                    if (Request.QueryString["Type"] != null)
                        type = int.Parse(Request.QueryString["Type"].ToString());
                    pn.Visible = true;
                    pnSP.Visible = false;
                    gvAt.DataSource = new cmsAlbumBL().SelectAll(type);
                    gvAt.DataBind();


                    Functions.ddlDatabinder(cboCategory, cmsCategoryDO.CATEGORYID_FIELD, cmsCategoryDO.TITLE_FIELD, new cmsCategoryBL().SelectAll());
                    cboCategory.Items.Insert(0, "Chọn tất cả -----------");
                }
                else
                {
                    pn.Visible = false;
                    pnSP.Visible = true;
                    grSP.DataSource = new cmsAlbumBL().SelectBySanPhamID(0, 0);
                    grSP.DataBind();
                }
            }


        }
        protected void gvArticle_StartRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e)
        {

        }
        protected void cboCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboCategory.SelectedIndex <= 0)
            {
                gvAt.DataSource = new cmsAlbumBL().SelectAll(type);
                gvAt.DataBind();
            }
            else
            {
                Functions.GvDatabinder(gvAt, new cmsAlbumBL().SelectByCategoryID(int.Parse(cboCategory.SelectedValue), type));
            }
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx?Page=Album&Type=" + type);
        }
        protected void gvAt_SelectedIndexChanged(object sender, EventArgs e)
        {
            int AlbumID = int.Parse(gvAt.DataKeys[gvAt.SelectedIndex].Value.ToString());
            Response.Redirect("Default.aspx?Page=AlbumDetail&AlbumID=" + AlbumID.ToString() + "&Type=" + type);
        }
        protected void gvAt_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            new cmsAlbumBL().Delete(new cmsAlbumDO { AlbumID = Convert.ToInt32(gvAt.DataKeys[e.RowIndex].Value) });
            Functions.Alert("Xóa bản tin thành công!", Request.Url.ToString());
        }

        protected void gvAt_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int id = int.Parse(e.CommandArgument.ToString());
            if (e.CommandName == "chitiet")
            {
                Response.Redirect("Default.aspx?Page=AlbumDetail&AlbumID=" + id.ToString() + "&Type=" + type);
            }
        }
        protected void gvSP_SelectedIndexChanged(object sender, EventArgs e)
        {
            int AlbumID = int.Parse(gvAt.DataKeys[gvAt.SelectedIndex].Value.ToString());
            Response.Redirect("Default.aspx?Page=AlbumDetail&AlbumID=" + AlbumID.ToString() + "&Type=" + 0);
        }
        protected void gvSP_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            new cmsAlbumBL().Delete(new cmsAlbumDO { AlbumID = Convert.ToInt32(gvAt.DataKeys[e.RowIndex].Value) });
            Functions.Alert("Xóa bản tin thành công!", Request.Url.ToString());
        }

        protected void gvSP_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int id = int.Parse(e.CommandArgument.ToString());
            if (e.CommandName == "chitiet")
            {
                Response.Redirect("Default.aspx?Page=AlbumDetail&AlbumID=" + id.ToString() + "&Type=" + 0);
            }
        }
        protected void btnAddSP_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx?Page=Album&Type=" + 0 + "&SP=1");
        }
    }
}